const mysql = require('mysql2/promise');
const axios = require('axios');

const pool = mysql.createPool({
  host: 'localhost',
  user: 'root',
  password: '123456',
  database: 'chatime'
});

const API_BASE = 'http://10.133.25.98:3000/api';

// 请根据实际情况填写
const senderId = 'user1';
const receiverId = 'user2';

async function main() {
  try {
    console.log('🔍 检查数据库 friend_requests 表...');
    const [requests] = await pool.execute(
      'SELECT * FROM friend_requests WHERE (sender_id=? OR receiver_id=?) ORDER BY create_time DESC',
      [senderId, receiverId]
    );
    if (requests.length === 0) {
      console.log('❌ 数据库中没有任何相关好友申请记录');
    } else {
      requests.forEach(r => {
        console.log(`  id=${r.id}, sender=${r.sender_id}, receiver=${r.receiver_id}, status=${r.status}, create_time=${r.create_time}`);
      });
      const pending = requests.filter(r => r.receiver_id === receiverId && r.status === 'pending');
      if (pending.length > 0) {
        console.log(`✅ 有${pending.length}条pending状态的好友申请`);
      } else {
        console.log('⚠️ 没有pending状态的好友申请');
      }
    }

    console.log('\n🌐 检查API接口返回...');
    const apiUrl = `${API_BASE}/friend/requests/${receiverId}`;
    const res = await axios.get(apiUrl);
    if (res.data && res.data.success) {
      const data = res.data.data;
      if (data.length === 0) {
        console.log('❌ API返回为空，页面不会显示任何申请');
      } else {
        data.forEach(r => {
          console.log(`  [API] id=${r.id}, sender=${r.sender_id}, receiver=${r.receiver_id}, status=${r.status}, create_time=${r.create_time}`);
        });
        console.log(`✅ API返回${data.length}条申请`);
      }
    } else {
      console.log('❌ API请求失败', res.data);
    }
  } catch (error) {
    console.error('❌ 检查失败:', error);
  } finally {
    await pool.end();
  }
}

main(); 